package priv.abadstring.study.haoke.webapi.manage.graphql;

import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import priv.abadstring.study.haoke.dubbo.dto.PageInfo;
import priv.abadstring.study.haoke.dubbo.entity.Ad;
import priv.abadstring.study.haoke.webapi.manage.AdManage;
import priv.abadstring.study.haoke.webapi.vo.ad.IndexAdResult;
import priv.abadstring.study.haoke.webapi.vo.ad.IndexAdResultData;

import java.util.ArrayList;
import java.util.stream.Collectors;

/**
 * IndexAdDataFetcher
 *
 * @author aBadString
 * @date 2021-06-18 20:36:37
 */
@Component
public class IndexAdDataFetcher implements DataFetcherSpecification<IndexAdResult> {
    @Autowired
    private AdManage adManage;

    @Override
    public String fieldName() {
        return "IndexAdList";
    }

    @Override
    public IndexAdResult dataFetcher(DataFetchingEnvironment args) {
        PageInfo<Ad> list = this.adManage.list(1, 1, 3);
        ArrayList<IndexAdResultData> adResultData = list.getRecords().stream().map(
                (ad) -> new IndexAdResultData(ad.getUrl())
        ).collect(Collectors.toCollection(() -> new ArrayList<>(list.getPageSize())));
        return new IndexAdResult(adResultData);
    }
}
